
package com.xin.di.uav.operate.mapper;

import com.xin.di.uav.common.response.IdAndNameResp;
import com.xin.di.uav.operate.po.ServiceAreaPO;
import com.xin.di.uav.user.order.po.MerchantPO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MerchantMapper {
    @Insert({"<script>",
            "INSERT INTO merchant (\n" +
                    "    id, \n" +
                    "    name, \n" +
                    "    address, \n" +
                    "    logo, \n" +
                    "    scenic_spot_id, \n" +
                    "    contact_person, \n" +
                    "    sort,\n" +
                    "    contact_info, \n" +
                    "    status\n" +
                    ")\n" +
                    "VALUES (\n" +
                    "    #{id},\n" +
                    "    #{name}, \n" +
                    "    #{address}, \n" +
                    "    #{logo}, \n" +
                    "    #{scenicSpotId}, \n" +
                    "    #{contactPerson}, \n" +
                    "    #{sort},\n" +
                    "    #{contactInfo}, \n" +
                    "    #{status}\n" +
                    ");\n",
            "</script>"})
    int insert(MerchantPO merchantPO);

    int deleteById(String id);

    // 动态字段更新（只更新非空字段）
    @Update({
            "<script>",
            "UPDATE merchant ",
            "<set>",
            "    <if test='scenicSpotId != null'>scenic_spot_id = #{scenicSpotId},</if>",
            "    <if test='status != null'>status = #{status},</if>",
            "    <if test='name != null'>name = #{name},</if>",
            "    <if test='address != null'>address = #{address},</if>",
            "    <if test='logo != null'>logo = #{logo},</if>",
            "    updated_at = NOW()",

            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int updateById(MerchantPO merchantPO);

    @Select({"<script>",
            "select id, name, address, logo, product_count, listed_products, scenic_spot_id, contact_person, sort,\n" +
                    "                      contact_info, created_at, updated_at, status \n" +
                        "from merchant where id =#{id} ",
            "</script>"})
    MerchantPO selectById(String id);


    @Select({"<script>",
            "select count(1) \n" +
                    "from merchant where 1=1 ",
            "    <if test='name != null and name !=\"\" '> and name like CONCAT('%', #{name}, '%')</if>",
            "</script>"})
    int count(@Param("name") String name);


    @Select({"<script>",
            "select id, name, address, logo, product_count, listed_products, scenic_spot_id, contact_person,\n" +
                    "                      contact_info, created_at, updated_at, status \n" +
                    " from merchant where 1=1 " +
                    "    <if test='name != null and name !=\"\" '> and name like CONCAT('%', #{name}, '%')</if>",
            " order by created_at desc   limit #{offset},#{pageSize} ",
            "</script>"})
    List<MerchantPO> selectList(@Param("offset") int offset, @Param("pageSize") int pageSize, @Param("name") String name);



    @Select({"<script>",
            "select id,name as name \n" +
                    " from merchant where  1=1 " +
                    "    <if test='serviceRegion != null and serviceRegion !=\"\"  '> and  name like CONCAT('%', #{serviceRegion}, '%') </if>",
            "order by created_at desc   ",
            "</script>"})
    List<IdAndNameResp> searchList(String serviceRegion);


    @Select({"<script>",
            "select id, name \n" +
                    "from merchant where id in ",
            "       <foreach item=\"id\" collection=\"ids\" open=\"(\" separator=\",\" close=\")\">\n" +
                    "            #{id}\n" +
                    "        </foreach>" +
                    "</script>"})
    List<IdAndNameResp> selectIdAndName(List<String> ids);
}
